﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Nissen_Model;

namespace Nissen_Dao
{
	public class SubjectCourseDao : DaoBase
	{
		public void SaveOrUpdateSubjectCourses(IList<SubjectCourse> subjectCourses,Test test)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
				using (ITransaction t1 = session.BeginTransaction())
				{
					ICriteria crt = session.CreateCriteria<SubjectCourse>();

					crt.Add(Restrictions.Eq("Test", test));

					IList<SubjectCourse> dbSubjectCourses = crt.List<SubjectCourse>();

					foreach (var dbSubjectCourse in dbSubjectCourses)
					{
						session.Delete(dbSubjectCourse);
					}

					
					foreach (var subjectCourse in subjectCourses)
					{
						session.Save(subjectCourse);
					}

					t1.Commit();
				}

			}
		}


		public IList<SubjectCourse> GetSubjectCourses(string tmpTestId)
		{
			using (ISession session = this.SessionFactory.OpenSession())
			{
			    try
			    {

                    ICriteria crt = session.CreateCriteria<SubjectCourse>();
                    crt.Add(Restrictions.Eq("Test.TestId", tmpTestId));

                    return crt.List<SubjectCourse>();
			    }
			    catch (ObjectNotFoundException)
			    {
                    return new List<SubjectCourse>();
			    }

			}
		}
	}
}
